package com.ktjiaoyu.utils.fastjson2;

import com.alibaba.fastjson2.filter.ValueFilter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * @Description
 * @Author 李楚鹏
 * @Date 2024/12/25 上午9:34
 **/

public class FastJson2DateValueFilter implements ValueFilter {
    public FastJson2DateValueFilter(LinkedHashMap<String,String> dateMappings){
        this.dateMappings=dateMappings;
    }
    private LinkedHashMap<String,String> dateMappings=null;
    @Override
    public Object apply(Object object, String propertyName, Object value) {
        if (dateMappings==null){
            return value;
        }
        if (value instanceof Date){
            Set<String> keySet=dateMappings.keySet();
            for (String pattern:keySet){
                if (propertyName.matches(pattern)){
                    String dateFormat=dateMappings.get(pattern);

                    SimpleDateFormat format=new SimpleDateFormat(dateFormat);
                    return format.format((Date)value);
                }
            }
        }
        return value;
    }
}
